<?php

namespace Aspic\Cache;

use Aspic as A;

abstract class AbstractFileCache extends AbstractCache {

	protected $_cacheFilesExtension;

	protected $_cacheDir;

	/**
	 *
	 * @param type $cacheDir
	 * @param type $performanceMode When true, cache data for a unique key
	 * will only be retrieve one time and save in memory for further demands.
	 */
	public function __construct($cacheDir, $performanceMode = true) {
		parent::__construct($performanceMode);

		$this->opt('cacheDir', $cacheDir);
		$this->opt('cacheFilesExtension', 'cache');
	}

	protected function setCacheDir($cacheDir) {
		if (!is_dir($cacheDir)) {
			throw new A\Exception('"' . $cacheDir . '" is not a valid directory');
		}
		else {
			$this->_cacheDir = $cacheDir;
		}
	}

	/**
	 * Should return a filename from a key
	 */
	protected function getKeyFileName($key) {
		return $key . '.' . $this->opt('cacheFilesExtension');
	}

	protected function getCacheFilePath($key) {
		$fn = $this->getKeyFileName($key);
		$fp = $this->opt('cacheDir') . '/' . $fn;

		return $fp;
	}

}
